package crm.model.teacherPC.net.job;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import crm.model.NetConst;
import crm.model.teacherPC.bannedList.ApplicationBanned;
import crm.model.teacherPC.bannedList.BrowserBanned;
import crm.model.teacherPC.net.PcWindow;
import crm.model.teacherPC.net.StudentPC;

public class WindowNameJob implements Job {
	
	private final BrowserBanned browserBannedList;
	private final ApplicationBanned appBannedList;
	private final StudentPC pc;
	private final boolean closeBrowser; 
	private final boolean closeApp;
	public WindowNameJob(BrowserBanned browserBannedList, ApplicationBanned appBannedList, StudentPC pc, boolean closeBrowser, boolean closeApp){
		this.browserBannedList = browserBannedList;
		this.appBannedList = appBannedList;
		this.pc = pc;
		this.closeApp = closeApp;
		this.closeBrowser = closeBrowser;
	}
	
	public Object doing(ObjectInputStream is, ObjectOutputStream os) {
		try {
			os.writeObject(NetConst.WINDOWS);
		} catch (IOException e) {

			e.printStackTrace();
		}
		int windowIndex = -1;
		ArrayList<PcWindow> nameList = new ArrayList<PcWindow>();
		try {
			windowIndex = (Integer)is.readObject();


			while(windowIndex != -1){
				PcWindow w = new PcWindow(windowIndex, is, os);
				
				nameList.add(w);

				windowIndex = (Integer)is.readObject();
			}
			for (PcWindow w : nameList) {
				w.init();
				if (closeBrowser){
					if (browserBannedList.match(w.getName())){
						w.kill();
					}
				}
				if (closeApp){
					if (appBannedList.match(w.getName())){
						w.kill();
					}
				}
				
				
			}

		} catch (IOException e) {

			e.printStackTrace();
		} catch (ClassNotFoundException e) {

			e.printStackTrace();
		}
		pc.setWindows(nameList);
		
		return null;



	}

}
